package cn.tianjindong.blog_manager.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.tianjindong.blog.common.pojo.PageView;
import cn.tianjindong.blog.mapper.BlogLinkMapper;
import cn.tianjindong.blog.pojo.BlogArticle;
import cn.tianjindong.blog.pojo.BlogArticleExample;
import cn.tianjindong.blog.pojo.BlogLink;
import cn.tianjindong.blog.pojo.BlogLinkExample;
import cn.tianjindong.blog_manager.service.LinkService;

@Service
public class LinkServiceImpl implements LinkService {

	@Autowired
	private BlogLinkMapper linkMapper;

	@Override
	public void addLink(BlogLink link, Long managerId) {
		link.setManagerId(managerId);
		linkMapper.insertSelective(link);
	}

	@Override
	public int getLinkNum() {
		int countByExample = linkMapper.countByExample(new BlogLinkExample());
		return countByExample;
	}

	@Override
	public PageView<BlogLink> getLinkPageView(int currentPage, int countItem) {
		// 查询时开始索引
		int startIndex = (currentPage - 1) * countItem;
		List<BlogLink> list = linkMapper.getLinkListLimit(startIndex, countItem);
		// 获取数据库总条数
		int totalItem = linkMapper.countByExample(new BlogLinkExample());
		
		PageView<BlogLink> pageView = new PageView<>();
		pageView.setCurrentPage(currentPage);
		pageView.setCountItem(countItem);
		pageView.setItems(list);
		pageView.setTotalItem(totalItem);
		
		return pageView;
	}

	@Override
	public BlogLink getLinkById(Long id) {
		BlogLink link = linkMapper.selectByPrimaryKey(id);
		return link;
	}

	@Override
	public void updateLink(BlogLink link) {
		linkMapper.updateByPrimaryKeySelective(link);
	}

	@Override
	public void deleteLink(Long id) {
		linkMapper.deleteByPrimaryKey(id);
	}

}
